package com.dzsw.comb;

import com.google.common.base.Joiner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import org.javatuples.Pair;

public class CombUtils {

    /**
     * 组合
     *
     * @param list
     * @param repeat 是否包含两个一样的数据
     * @return
     */
    public static <T> List<Pair<T, T>> combineTwo(List<T> list, boolean repeat) {
        List<Pair<T, T>> resultList = new ArrayList<>();
        for (int offset = 0; offset < list.size(); offset++) {
            T first = list.get(offset);
            int tmp = offset;
            if (!repeat) {
                tmp = offset + 1;
            }
            for (int i = tmp; i < list.size(); i++) {
                T second = list.get(i);
                resultList.add(new Pair<>(first, second));
            }
        }
        return resultList;
    }

    /**
     * 排列
     *
     * @param list
     * @param repeat 是否包含两个一样的数据
     * @return
     */
    public static <T> List<Pair<T, T>> permutationTwo(List<T> list, boolean repeat) {
        List<Pair<T, T>> resultList = new ArrayList<>();
        for (T first : list) {
            for (T second : list) {
                if (repeat) {
                    resultList.add(new Pair<>(first, second));
                } else {
                    if (!Objects.equals(first, second)) {
                        resultList.add(new Pair<>(first, second));
                    }
                }
            }
        }
        return resultList;
    }

    public static void main(String[] args) {
        List<String> list = Arrays.asList("a", "b", "c", "d", "e");
        List<Pair<String, String>> l = CombUtils.combineTwo(list, false);
        System.out.println(l.size());
        String ss = Joiner.on(",").join(l);
        System.out.println(ss);
        System.out.println("==========================================");
        l = permutationTwo(list, false);
        ss = Joiner.on(",").join(l);
        System.out.println(l.size());
        System.out.println(ss);
    }
}
